﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using G15_Agent.Models;

namespace G15_Agent.Helpers
{
    static public class DatabaseHelper
    {
        static private AgentContext DatabaseSQL = new AgentContext();
        public enum TrangThaiHoaDon : int
        {
            New = 0,
            Pending = 1,
            Paid = 2,
            ServiceCancel = 3,
            UserCancel = 4,
            AgentCancel = 5
        }

        static public string GetServiceName(string serviceID)
        {
            try 
            { 
                return DatabaseSQL.DanhSachNCCs.FirstOrDefault(m => m.MaNCC == serviceID).TenNCC;
            } catch (Exception ex)
            {
                return null;
            }
        }

        static public string GetUserID(string username)
        {
            try
            {
                return DatabaseSQL.NguoiDungs.FirstOrDefault(m => m.TenDangNhap == username).MaNguoiDung;
            }
            catch (Exception ex)
            {
                return null;
            }
            
        }

        static public HoaDonNCC GetServiceOrder(string serviceID)
        {
            HoaDonNCC result = DatabaseSQL.HoaDonNCCs.FirstOrDefault(m => m.MaNCC == serviceID 
                && m.TrangThai == (int)TrangThaiHoaDon.Pending);

            if (result == null)
            {
                result = new HoaDonNCC();
               
                result.MaHoaDon = "HNCC" + (DatabaseSQL.HoaDonNCCs.Count() + 1).ToString("000000000");
                result.MaNCC = serviceID;
                result.NgayLap = System.DateTime.Today;
                result.TrangThai = (int)TrangThaiHoaDon.Pending;
                result.DonGia = 0;

                DatabaseSQL.HoaDonNCCs.Add(result);
                DatabaseSQL.SaveChanges();
            }

            return result;
        }

        static public HoaDonKH GetUserOrder(string userID)
        {
            HoaDonKH result = DatabaseSQL.HoaDonKHs.FirstOrDefault(m => m.MaNguoiDung == userID 
                && m.TrangThai == (int)TrangThaiHoaDon.Pending);

            if (result == null)
            {
                result = new HoaDonKH();
                result.MaHoaDon = (DatabaseSQL.HoaDonKHs.Count() + 1).ToString();
                result.MaNguoiDung = userID;
                result.NgayLap = System.DateTime.Today;
                result.TrangThai = (int)TrangThaiHoaDon.Pending;
                result.DonGia = 0;

                DatabaseSQL.HoaDonKHs.Add(result);
                DatabaseSQL.SaveChanges();
            }

            return result;
        }

        static public void AddOrderDetail(string serviceID, string userID, string itemDetail, int price)
        {
            ChiTietHDKH chiTiet = new ChiTietHDKH();
            HoaDonNCC hoaDonNCC = GetServiceOrder(serviceID);
            HoaDonKH hoaDonKH = GetUserOrder(userID);

            chiTiet.MaHD_NCC = hoaDonNCC.MaHoaDon;
            chiTiet.HoaDonNCC = hoaDonNCC;
            chiTiet.MaHD_KH = hoaDonKH.MaHoaDon;
            chiTiet.HoaDonKH = hoaDonKH;
            chiTiet.ThongTinDV = itemDetail;
            chiTiet.DonGia = price;
            chiTiet.TrangThai = (int)TrangThaiHoaDon.Pending;
            chiTiet.MaChiTietHD = chiTiet.MaHD_KH + chiTiet.MaHD_NCC + 
                (DatabaseSQL.ChiTietHDKHs.Count(m => m.MaHD_KH == chiTiet.MaHD_KH 
                    && m.MaHD_NCC == chiTiet.MaHD_NCC) + 1).ToString();

            DatabaseSQL.ChiTietHDKHs.Add(chiTiet);
            //hoaDonKH.DonGia += chiTiet.DonGia;
            //hoaDonNCC.DonGia += chiTiet.DonGia;
            DatabaseSQL.SaveChanges();
        }
    }
}